package com.tianqu.tms.www.proxy.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.entity.TmsProxyPic;
import com.tianqu.tms.www.proxy.viewhelper.CarProxyInputViewHelper;

/**
 * 
 * 未受理修改录入结果页面Action
 * 
 * @author Administrator
 * 
 */
public class CarProxyNPayedUpdateResultController extends TmsController {

	private Logger logger = Logger
			.getLogger(CarProxyNPayedUpdateResultController.class);

	private static final String SYSPATH = "sysPath";//

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = getTxController();
		try {
			controller.begin();

			CarProxyInputViewHelper view = (CarProxyInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			uc.removeAttribute(TmsConstants.VIEWHELP);
			ProxyCarInfo proxy = new ProxyCarInfo();
			proxy = setEntityFromViewhelper(proxy, view);

			TmsUserProfile profile = uc.getUserProfile();

			proxy.setUpdateUserId(profile.getUniqueId());

			Codes code = Codes.getInstance();
			proxy.updateNCarInfo(proxy, code.getItemCodeByItemIdAndItemValue(
					Codes.PROXYCAR_STATUS, view.getStatus()), view.getSerial());

			String sysPath = (String) uc.getAttribute(SYSPATH);

			TmsProxyPic pic = new TmsProxyPic();

			pic.setMaterialImages1(view.getMaterialImages1());// 资料图片
			pic.setMaterialImages2(view.getMaterialImages2());
			pic.setMaterialImages3(view.getMaterialImages3());
			pic.setMaterialImages4(view.getMaterialImages4());
			pic.setMaterialImages5(view.getMaterialImages5());
			pic.setOwnersGroupPhoto(view.getOwnersGroupPhoto());
			pic.setSerial(view.getSerial());
			pic.setUpdateUserId(profile.getUniqueId());
			pic.setInsertUserId(profile.getUniqueId());

			TmsProxyPic entityPic = new TmsProxyPic();
			entityPic = entityPic.findPicInfoBySerial(view.getSerial());

			if (entityPic == null) {
				pic.insertProxyPicInfo(pic, sysPath);
			} else {
				pic.updateProxyInfo(pic, sysPath);
			}

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 
	 * 将viewhelp中值set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private ProxyCarInfo setEntityFromViewhelper(ProxyCarInfo entity,
			CarProxyInputViewHelper view) throws TmsDaoException {
		Codes code = Codes.getInstance();
		entity.setSerial(view.getSerial());
		entity.setVin(view.getVin());
		entity.setEngine(view.getEngine());
		entity.setLicensePlate(view.getLicensePlate());
		entity.setColor(code.getItemCodeByItemIdAndItemValue(
				Codes.VEHICLE_COLOR, view.getColor()));
		entity.setRegDate(view.getRegDate());
		entity.setYearCarefulPeriod(view.getYearCarefulPeriod());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());
		if (view.getDisplacement() != null
				&& !"".equals(view.getDisplacement())) {
			entity.setDisplacement(Integer.parseInt(view.getDisplacement()));
		} else {
			entity.setDisplacement(0);
		}
		entity.setJqx(view.getJqx());
		entity.setCarUse(code.getItemCodeByItemIdAndItemValue(
				Codes.USER_NATURE, view.getCarUse()));
		entity.setCarType(code.getItemCodeByItemIdAndItemValue(
				Codes.VEHICLE_TYPE, view.getCarType()));
		entity.setGearbox(code.getItemCodeByItemIdAndItemValue(
				Codes.GEARBOX_TYPE, view.getGearbox()));
		if (view.getMileage() != null && !"".equals(view.getMileage())) {
			entity.setMileage(Integer.parseInt(view.getMileage()));
		} else {
			entity.setMileage(0);
		}
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCardType(code.getItemCodeByItemIdAndItemValue(
				Codes.PLATE_TYPE, view.getCardType()));
		entity.setRegisterCardNo(view.getRegisterCardNo());
		entity.setCountryStyle(code.getItemCodeByItemIdAndItemValue(
				Codes.VEHICLE_COUNTRY, view.getCountryStyle()));
		entity.setFuelType(code.getItemCodeByItemIdAndItemValue(
				Codes.FUEL_TYPE, view.getFuelType()));
		entity.setCompanyContactPerson(view.getCompanyContactPerson());
		entity.setCarOwnerIdCardNo(view.getCarOwnerIdCardNo());

		Member member = Member.getInstance();

		entity.setMemberId(String.valueOf(member.getMemIdByMemName(view
				.getMemberId())));

		if (view.getLoadquality() != null && !"".equals(view.getLoadquality())) {
			entity.setLoadquality(Integer.parseInt(view.getLoadquality()));
		} else {
			entity.setLoadquality(0);
		}
		if (view.getPassenger() != null && !"".equals(view.getPassenger())) {
			entity.setPassenger(Integer.parseInt(view.getPassenger()));
		} else {
			entity.setPassenger(0);
		}

		entity.setOwnerType(code.getItemCodeByItemIdAndItemValue(
				Codes.CAR_OWNER, view.getOwnerType()));
		entity.setOwnerPhone(view.getOwnerPhone());
		entity.setOwnerAddress(view.getOwnerAddress());
		entity.setOwnerOrgNo(view.getOwnerOrgNo());
		entity.setCarOwner(view.getCarOwner());
		entity.setCarOwnerIdCardNoOld(view.getCarOwnerIdCardNoOld());
		entity.setOwnerAddressOld(view.getOwnerAddressOld());
		entity.setAgentName(view.getAgentName());
		entity.setAgentOwnerIdCardNo(view.getAgentOwnerIdCardNo());
		entity.setAgentAddress(view.getAgentAddress());
		entity.setAgentPhone(view.getAgentPhone());
		entity.setMoveOutProvince(view.getMoveOutProvince());
		entity.setMoveOutCity(view.getMoveOutCity());
		entity.setBrandId(view.getBrandId());
		entity.setCarStyleId(view.getCarStyleId());
		entity.setVendorId(view.getVendorId());

		return entity;
	}
}
